<?php
/**
 * Created by PhpStorm.
 * User: l-ht
 * Date: 2018/10/28
 * Time: 09:51
 */

namespace app\index\controller;

use app\index\model\User;
use think\Controller;
use think\Db;

class Base extends Controller
{

    //成功响应
    public function _toSuccess($data = [])
    {
        $data['code'] = 200;
        return json()->data($data,200);
    }

    //失败响应
    public function _toError($data = [])
    {
        $data['code'] = 500;
        return json()->data($data,500);

    }

    //过滤表情
    public function filter_Emoji($str)
    {
        $str = preg_replace_callback(    //执行一个正则表达式搜索并且使用一个回调进行替换
            '/./u',
            function (array $match) {
                return strlen($match[0]) >= 4 ? '' : $match[0];
            },
            $str);

        return $str;
    }

    //积分操作
    public function makePoint($uid,$tid,$type)
    {

        $point = 0;

        $type == 1 && $point = 10; //1、成功发布一贴  奖励10分
        $type == 2 && $point = 30; //2、发帖加为精华  奖励30分
        $type == 3 && $point = 10; //3、转发一贴     奖励10分
        $type == 4 && $point = 2;  //4、每点赞一次    奖励2分
        $type == 5 && $point = 2;  //5、每评论一次    奖励2分
        $type == 6 && $point = 10; //6、投诉成功一次  奖励10分
        $type == 7 && $point = 20; //7、被投诉成功一次 扣除20分
        $type == 8 && $point = 10; //8、被管理员删除   扣10分
        $type == 9 && $point = 10; //9、处罚一次       扣10分
        $type == 10 && $point = 10; //10、删帖          扣10分
		$type == 11 && $point = 2; //11、取消点赞      扣2分
        //奖励积分
        if ($type <= 6 && $type >= 1) {
            User::where('id',$uid)->setInc('point',$point);
        }

        //扣除积分
        if ($type >=7 && $type <= 11) {
            User::where('id',$uid)->setDec('point',$point);
        }

        //写入记录表
        Db::name("point")->insert([
            'uid'           => $uid,
            'type'          => $type,
            'num'           => $point,
            'tid'           => $tid,
            'create_time'   => time(),
        ]);
    }
	//xx时间前  时间戳 计算  天 时 分 秒
	public function lastime($lastime)
	{
		$ftime = time()-$lastime;
					if(floor($ftime/86400)!=0){
						$rstr  = floor($ftime/86400)."天前";
					}elseif(floor($ftime/3600)!=""){
						$rstr  = floor($ftime/3600)."小时前";
					}elseif(floor($ftime/60)!=""){
						$rstr  = floor($ftime/60)."分钟前";
					}else{
						$rstr  = $ftime."秒前";
					}
					return $rstr;
	}
	
}